// QuicksView.h : interface of the CQuicksView class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "atlcrack.h"
#include "WindowManager.h"
#include "IdoComplete.h"
using namespace quicks;

#define ID_REFRESH_RESULTS                   66666

class CQuicksView : public CWindowImpl<CQuicksView, CRichEditCtrl>
{
public:
	CQuicksView();
	DECLARE_WND_SUPERCLASS(NULL, CRichEditCtrl::GetWndClassName())

	BOOL PreTranslateMessage(MSG* pMsg);

	BEGIN_MSG_MAP(CQuicksView)
		MSG_WM_CREATE(OnCreate)
		MSG_WM_CHAR(OnChar)
		MSG_WM_KEYDOWN(OnKeyDown)
		MSG_WM_KEYUP(OnKeyUp)
		MSG_WM_DESTROY(OnDestroy)
		MSG_WM_HOTKEY(OnHotKey)
		MESSAGE_HANDLER(ID_REFRESH_RESULTS, OnRefresh)


		// these don't really work.
		NOTIFY_CODE_HANDLER(EN_PROTECTED, OnProtected)
		COMMAND_ID_HANDLER(ID_NEXT_MATCH, OnNextMatch)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	int OnCreate(LPCREATESTRUCT lpCreateStruct);
	void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnDestroy();
	void OnHotKey(int nHotKeyID, UINT uModifiers, UINT uVirtKey);

	LRESULT OnProtected(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/);

private:
	void GetResultSel(CHARRANGE& sel);
	void GetUserSel(CHARRANGE& sel);
	void RefreshResultsDisplay(bool force=false);
	void RefreshResults();
	void Clear();

	WindowManager m_wm;
	int m_totalLength;
	int m_resultsLength;
	static const int MAXTEXT = 1024;
	TCHAR m_text[MAXTEXT];
	std::vector<WindowManager::WindowDesc> m_shownWindows;
	IdoComplete<WindowManager::WindowDesc> m_ic;
	IdoComplete<WindowManager::WindowDesc>::MatchResult m_matchResult;
	bool m_init;
	bool m_updating;
	HFONT m_hfRegular;
	HFONT m_hfbold;
	wstring m_lastUserInput;
public:
	LRESULT OnNextMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnPrevMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnRefresh(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};
